import { ProTableColumn } from '@/components/pro-table/type'
import { NButton, NPopconfirm } from 'naive-ui'
import { InventoryModel } from '@/api/model/inventory-model'
import { userInfo } from '~/types/user'
import { userInfoModel } from '@/api/model/userModel'
import { Ref } from 'vue'

type Props = {
  userInfo: Ref<userInfo<userInfoModel>>
  handleEditInventory: (row: InventoryModel, type: 'edit' | 'check') => void
  fetchDelInventory: (id: number) => void
}

export const useInventoryProTable = ({
  userInfo,
  handleEditInventory,
  fetchDelInventory
}: Props) => {
  const router = useEncryptRouter()
  //  *  const { id } = router.getQuery()
  const columns: ProTableColumn<InventoryModel>[] = [
    {
      title: '盘点编号',
      key: 'inventoryCode',
      query: {
        type: 'input'
      }
    },
    {
      title: '盘点名称',
      key: 'inventoryName',
      query: {
        type: 'input'
      }
    },
    {
      title: '盘点资产总数',
      key: 'inventoryNumber'
    },
    {
      title: '盘点人',
      key: 'inventoryPerson',
      query: {
        type: 'input'
      }
    },
    {
      title: '申请人',
      key: 'receivePerson'
    },
    {
      title: '申请部门',
      key: 'receiveBranch'
    },
    {
      title: '申请日期',
      key: 'receiveDate'
    },
    {
      title: '备注',
      key: 'remark'
    },
    {
      type: 'tag',
      title: '盘点状态',
      key: 'inventoryType',
      dictOptions: useDictData('INVENTORY_TYPE').value,
      query: {
        type: 'select',
        dictOptions: useDictData('INVENTORY_TYPE').value
      }
    },
    {
      title: '操作',
      key: 'operation',
      render(row) {
        return (
          <>
            {row.inventoryType !== '1' ? (
              <NButton
                type="primary"
                size="small"
                onClick={() => handleEditInventory(row, 'edit')}
                quaternary
              >
                修改
              </NButton>
            ) : (
              ''
            )}

            {row.inventoryType !== '1' &&
            row.inventoryPersonId === userInfo.value?.userId.toString() ? (
              <NButton
                type="info"
                size="small"
                onClick={() => handleInventory(row, 'edit')}
                quaternary
              >
                盘点
              </NButton>
            ) : (
              ''
            )}

            {row.inventoryType === '1' ? (
              <NButton
                type="primary"
                size="small"
                onClick={() => handleEditInventory(row, 'check')}
                quaternary
              >
                详情
              </NButton>
            ) : (
              ''
            )}
            {row.inventoryType === '1' ? (
              <NButton
                type="success"
                size="small"
                onClick={() => handleInventory(row, 'detail')}
                quaternary
              >
                盘点详情
              </NButton>
            ) : (
              ''
            )}

            {row.inventoryType !== '1' ? (
              <NPopconfirm onPositiveClick={() => fetchDelInventory(row.id)}>
                {{
                  trigger: () => (
                    <NButton size="small" type="error" quaternary>
                      删除
                    </NButton>
                  ),
                  default: () => '您确定要删除？'
                }}
              </NPopconfirm>
            ) : (
              ''
            )}
          </>
        )
      }
    }
  ]

  // 跳转到盘点详情
  const handleInventory = (row: any, type: 'edit' | 'detail') => {
    router.push({
      name: 'InventoryDetails',
      query: {
        type: type,
        code: row.inventoryCode,
        title: row.inventoryName
      }
    })
  }

  return {
    columns
  }
}
